package xu.ye.view;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import xu.ye.bean.TypeBean;
import xu.ye.uitl.Cn2Spell;
import xu.ye.view.adapter.ContactTypeAdapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView.ScaleType;
import android.widget.ViewSwitcher.ViewFactory;

import com.agent.crm.db.DBInitScript;
import com.agent.crm.db.DatabaseManager;
import com.agent.crm.util.CnToSpell;
import com.agent.crm.view.R;
//import com.zhengping.contact.db.DBHelper;
//import com.zhengping.contact.entity.User;
public class AddNewActivity extends Activity implements ViewFactory {
    EditText et_contactId;
	EditText et_name;
	EditText et_mobilePhone;
	EditText et_officePhone;
	EditText et_familyPhone;
	EditText et_position;
	EditText et_company;
	EditText et_address;
	EditText et_zipCode;
	EditText et_otherContact;
	EditText et_email;
	EditText et_remark;
	EditText et_birthDate;
	Button btn_save;
	Button btn_return;
	RadioGroup sexRadioGroup;
	RadioButton btn_male;
	RadioButton btn_female;
	Spinner sp_workType;
	TypeBean selectedTB = new TypeBean(); 
	
	//性别选择：0 无 1男 2女
	String sexChecked = "0";
	
    private ContactTypeAdapter typeAdapter;
	
	
	int privacy;//用于判断添加的用户是不是保密的
    ImageButton imageButton;//头像按钮
    View imageChooseView;//图像选择的视图
    AlertDialog imageChooseDialog;//头像选择对话框
    Gallery gallery;//头像的Gallery
    ImageSwitcher is;//头像的ImageSwitcher
    int currentImagePosition;//用于记录当前选中图像在图像数组中的位置
    int previousImagePosition;//用于记录上一次图片的位置
    boolean imageChanged;//判断头像有没有变化
    
	private DatabaseManager manager = null;
	
	private int mYear;  
	  
    private int mMonth;  
  
    private int mDay; 
	
    private static final int DATE_DIALOG_ID = 1;  
    private static final int SHOW_DATAPICK = 0;  
    
    private List<TypeBean> typeList = new ArrayList<TypeBean>();
    
	/**
     * 所有的图像图片
     */
//	private  int[] images 
//			= new int[]{R.drawable.icon
//		,R.drawable.image1,R.drawable.image2,R.drawable.image3
//		,R.drawable.image4,R.drawable.image5,R.drawable.image6
//		,R.drawable.image7,R.drawable.image8,R.drawable.image9
//		,R.drawable.image10,R.drawable.image11,R.drawable.image12
//		,R.drawable.image13,R.drawable.image14,R.drawable.image15
//		,R.drawable.image16,R.drawable.image17,R.drawable.image18
//		,R.drawable.image19,R.drawable.image20,R.drawable.image21
//		,R.drawable.image22,R.drawable.image23,R.drawable.image24
//		,R.drawable.image25,R.drawable.image26,R.drawable.image27
//		,R.drawable.image28,R.drawable.image29,R.drawable.image30};
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addnew);
		
		manager = new DatabaseManager(this);
		Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);
		
		
		Intent intent = getIntent();
		//先确定好保密类型
		if(intent.getExtras()!=null && intent.getExtras().getInt("privacy") == 1) {
			privacy = 1;
		} else {
			privacy = 0;
		}
		
		et_contactId = (EditText)findViewById(R.id.editContactId);
		et_name = (EditText)findViewById(R.id.username);
		et_mobilePhone = (EditText)findViewById(R.id.mobilephone);
		et_officePhone = (EditText)findViewById(R.id.officephone);
		et_familyPhone = (EditText)findViewById(R.id.familyphone);
		et_position = (EditText)findViewById(R.id.position);
		et_company = (EditText)findViewById(R.id.company);
		et_address = (EditText)findViewById(R.id.address);
		et_email = (EditText)findViewById(R.id.email);
		et_remark = (EditText)findViewById(R.id.remark);
		et_birthDate = (EditText)findViewById(R.id.contactBirthDate);
		et_birthDate.setOnClickListener(new DateButtonOnClickListener());
		et_birthDate.setFocusable(false);
		sexRadioGroup = (RadioGroup) findViewById(R.id.sexRadioGroup);
		sexRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
              //获取变更后的选中项的ID
                int radioButtonId = group.getCheckedRadioButtonId();
                //根据ID获取RadioButton的实例
                 RadioButton rb = (RadioButton)AddNewActivity.this.findViewById(radioButtonId);
                 if(rb.getText().toString().equals("男")){
                     sexChecked = "1";
                 }else{
                     sexChecked = "2";
                 }
                 
            }
        });
		btn_male = (RadioButton) findViewById(R.id.radioMale);
		btn_female = (RadioButton) findViewById(R.id.radioFemale);
		sp_workType = (Spinner) findViewById(R.id.workTypeSpinner);
		
		SQLiteDatabase database = manager.getWritableDatabase();
		Cursor cursor = database.query(DBInitScript.Table_TypeInfo, null, null, null, null, null, null);
		int count = cursor.getCount();
		int index = -1;
		if(count > 0){
		    for(int i = 0; i < cursor.getCount(); i++){
		        cursor.moveToPosition(i);
		        index = cursor.getColumnIndex("type_id");
		        int typeId = cursor.getInt(index);
		        index = cursor.getColumnIndex("type_name");
                String typeName = cursor.getString(index);
                index = cursor.getColumnIndex("sys_config");
                int sysConfig = cursor.getInt(index);
                TypeBean tb = new TypeBean();
                tb.setTypeId(typeId);
                tb.setTypeName(typeName);
                tb.setSysConfig(sysConfig);
                typeList.add(tb);
		    }
		    typeAdapter = new ContactTypeAdapter(this, typeList);
	        sp_workType.setAdapter(typeAdapter);
	        sp_workType.setOnItemSelectedListener(new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    selectedTB = typeList.get(position);
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub
                }
	            
	        });
		}
		
		
		
		Boolean bool = intent.getBooleanExtra("edit", false);
		if(bool == true){
		    et_contactId.setText(intent.getIntExtra("contacts_id", -1) + "");
		    et_name.setText(intent.getStringExtra("name")); 
		    et_mobilePhone.setText(intent.getStringExtra("mobile_phone"));
		    et_officePhone.setText(intent.getStringExtra("office_phone"));
		    et_familyPhone.setText(intent.getStringExtra("family_phone"));
		    et_position.setText(intent.getStringExtra("job"));
		    et_company.setText(intent.getStringExtra("company"));
		    et_address.setText(intent.getStringExtra("address"));
		    et_email.setText(intent.getStringExtra("email"));
		    et_remark.setText(intent.getStringExtra("remarks"));
		    et_birthDate.setText(intent.getStringExtra("birth_date"));
		    String sex = intent.getStringExtra("sex");
		    if(!TextUtils.isEmpty(sex)){
		        if(sex.equals("1")){
		            btn_male.setChecked(true);
		        }
		        else if(sex.equals("2")){
		            btn_female.setChecked(true);
		        }
		    }
		    intent.getIntExtra("type_id", -1);
		    
		}
		
		btn_save = (Button)findViewById(R.id.save);
		btn_return = (Button)findViewById(R.id.btn_return);
		imageButton = (ImageButton)findViewById(R.id.image_button); 
		
		
		final String[] projection = { 
                ContactsContract.CommonDataKinds.Phone._ID,
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.DATA1,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
                ContactsContract.CommonDataKinds.Phone.PHOTO_ID,
                ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY
        }; // 查询的列
		/**
		 * ��Ӧ����¼�
		 */
		btn_save.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				//判断姓名是否为空
                String name = et_name.getText().toString();
                if(name.trim().equals("")) {
                    Toast.makeText(AddNewActivity.this, "姓名不许为空", Toast.LENGTH_LONG).show();
                    return;
                }
                //从表单上获取数据
//                if(imageChanged) {
//                    user.imageId = images[currentImagePosition%images.length];
//                } else {
//                    user.imageId = images[previousImagePosition%images.length];
//                }
//                user.privacy = privacy;
                //创建数据库帮助类
                SQLiteDatabase sqlLite = manager.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("name", name);
                values.put("address", et_address.getText().toString());
                values.put("email", et_email.getText().toString());
                values.put("company", et_company.getText().toString());
                values.put("job", et_position.getText().toString());
                values.put("remarks", et_remark.getText().toString());
                values.put("family_phone", et_familyPhone.getText().toString());
                values.put("office_phone", et_officePhone.getText().toString());
                values.put("mobile_phone", et_mobilePhone.getText().toString());
                values.put("birth_date", et_birthDate.getText().toString());
                values.put("sex", sexChecked);
                
                Intent intent = getIntent();
                Boolean bool = intent.getBooleanExtra("edit", false);
                ContentResolver contentResolver = getContentResolver();
                ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
                int sysContactId = -1;
                int contactId = -1;
                long phoneId = -1;
                String lookupKey = "";
                String sortKey = CnToSpell.getFirstSpell(name);
                //更新
                if(bool){
                    sysContactId = intent.getIntExtra("sys_contactid", -1);
                    contactId = intent.getIntExtra("contacts_id", -1);
                    ContentProviderOperation operation1 = ContentProviderOperation
                            .newUpdate(ContactsContract.Data.CONTENT_URI)
                            .withSelection(Data.RAW_CONTACT_ID + "=?"+" AND " + 
                            ContactsContract.Data.MIMETYPE + " = ?",
                            new String[]{String.valueOf(sysContactId),StructuredName.CONTENT_ITEM_TYPE})
                            .withValue(StructuredName.DISPLAY_NAME, name)
                            .build(); 
                    operations.add(operation1);
                    
                    ContentProviderOperation operation2 = ContentProviderOperation
                            .newUpdate(ContactsContract.Data.CONTENT_URI)
                            .withSelection(Data.RAW_CONTACT_ID + "=?"+" AND " + 
                            ContactsContract.Data.MIMETYPE + " = ?" + " AND " +
                                    Phone.TYPE+"=?",
                            new String[]{String.valueOf(sysContactId),
                                    Phone.CONTENT_ITEM_TYPE,String.valueOf(Phone.TYPE_MOBILE)})
                            .withValue(Phone.NUMBER, et_mobilePhone.getText().toString())
                            .build(); 
                    operations.add(operation2);
                    
                    ContentProviderOperation operation3 = ContentProviderOperation
                            .newUpdate(ContactsContract.Data.CONTENT_URI)
                            .withSelection(Data.RAW_CONTACT_ID + "=?"+" AND " + 
                            ContactsContract.Data.MIMETYPE + " = ?" + " AND " +
                                    Email.TYPE+"=?",
                            new String[]{String.valueOf(sysContactId),Email.CONTENT_ITEM_TYPE,String.valueOf(Email.TYPE_WORK)})
                            .withValue(Email.DATA, et_email.getText().toString())
                            .build(); 
                    operations.add(operation3);
                    try {
                        contentResolver.applyBatch(ContactsContract.AUTHORITY, operations);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    } catch (OperationApplicationException e) {
                        e.printStackTrace();
                    }
                    
                    int result = sqlLite.update(DBInitScript.Table_ContactsInfo, values, "contacts_id=?", new String[]{String.valueOf(contactId)});
                    if(result == -1){
                        Toast.makeText(AddNewActivity.this, "更新失败", Toast.LENGTH_LONG);
                    }
                    
                    setTitle("用户更新成功");
                    
                    
                }
                //新增
                else {
                    ContentProviderOperation operation1 = ContentProviderOperation
                            .newInsert(RawContacts.CONTENT_URI)
                            .withValue(Data._ID, null)
                            .build();
                    operations.add(operation1);
                    
                    ContentProviderOperation operation2 = ContentProviderOperation
                            .newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValueBackReference(Data.RAW_CONTACT_ID, 0) // 
                            .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE) 
                            .withValue(StructuredName.DISPLAY_NAME, name)
                            .build(); 
                    operations.add(operation2); 

                    ContentProviderOperation operation3 = ContentProviderOperation
                            .newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValueBackReference(Data.RAW_CONTACT_ID, 0)
                            .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                            .withValue(Phone.NUMBER, et_mobilePhone.getText().toString())
                            .withValue(Phone.TYPE, Phone.TYPE_MOBILE)
                            .build();
                    operations.add(operation3); 
                    
                    ContentProviderOperation operation4 = ContentProviderOperation
                            .newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValueBackReference(Data.RAW_CONTACT_ID, 0)
                            .withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
                            .withValue(Email.DATA, et_email.getText().toString())
                            .withValue(Email.TYPE, Email.TYPE_WORK)
                            .build();
                    operations.add(operation4);
                    long rawContactId = 0l;
                    try {
                        ContentProviderResult[] conProRes = contentResolver.applyBatch(ContactsContract.AUTHORITY, operations);
                        rawContactId = ContentUris.parseId(conProRes[0].uri);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    } catch (OperationApplicationException e) {
                        e.printStackTrace();
                    }
                    
                    Cursor cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, 
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[]{rawContactId + ""}, null);
                    for(int i = 0; i < cursor.getCount(); i++){
                        cursor.moveToPosition(i);
                        sysContactId = cursor.getInt(3);
                        phoneId = cursor.getLong(4);
                        lookupKey = cursor.getString(5);
                        values.put("sys_contactid", sysContactId);
                        values.put("photoid", phoneId);
                        values.put("lookup_key", lookupKey);
                    }
                    values.put("sort_key", sortKey);
                    long result = sqlLite.insert(DBInitScript.Table_ContactsInfo, null, values);
                    
                    //通过结果来判断是否插入成功，若为1，则表示插入数据失败
                    if(result == -1 ) {
                        Toast.makeText(AddNewActivity.this, "添加失败", Toast.LENGTH_LONG);
                    }
                    contactId = (int)result;
                    setTitle("用户添加成功!");
                }
                //插入分类选择
                int typeId = selectedTB.getTypeId();
                sqlLite.delete(DBInitScript.Table_Type_Middle, "contactsid=?", new String[]{contactId + ""});
                ContentValues typeValue = new ContentValues();
                typeValue.put("contactsid", contactId);
                typeValue.put("typeid", typeId);
                sqlLite.insert(DBInitScript.Table_Type_Middle, null, typeValue);
                
                Bundle bundle = new Bundle();
                bundle.putString("name", name);
                bundle.putString("sort_key", sortKey);
                bundle.putString("lookup_key", lookupKey);
                bundle.putLong("photoid", phoneId);
                bundle.putString("address", et_address.getText().toString());
                bundle.putString("email", et_email.getText().toString());
                bundle.putString("company", et_company.getText().toString());
                bundle.putString("job", et_position.getText().toString());
                bundle.putString("remarks", et_remark.getText().toString());
                bundle.putString("family_phone",  et_familyPhone.getText().toString());
                bundle.putString("office_phone", et_officePhone.getText().toString());
                bundle.putString("mobile_phone", et_mobilePhone.getText().toString());
                bundle.putInt("contacts_id", contactId);
                bundle.putInt("sys_contactid", sysContactId);
                bundle.putString("birth_date", et_birthDate.getText().toString());
                bundle.putString("sex", sexChecked);
                
                Intent editIntent = new Intent();
                editIntent.putExtras(bundle);
                if(bool){
                    setResult(4, editIntent);
                }else{
                    setResult(3, editIntent);
                }
                
                
                
                
                sqlLite.close();
                //销毁当前视图
                finish();
			}
			
		});
		
		btn_return.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				finish();
			}
		});
		
		imageButton.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				
				//loadImage();//Ϊgalleryװ��ͼƬ
				initImageChooseDialog();//��ʼ��imageChooseDialog
				imageChooseDialog.show();
			}
		});
		
	}
	
	class DateButtonOnClickListener implements  
	  
        android.view.View.OnClickListener {  
    
        @Override  
        
        public void onClick(View v) {  
        
            Message msg = new Message();  
        
            if (et_birthDate.equals((EditText) v)) {  
        
               msg.what = AddNewActivity.SHOW_DATAPICK;  
        
            }  
        
            AddNewActivity.this.saleHandler.sendMessage(msg);  
        
        }  
    
    }  
	
	
	/** 
	 
     * 更新日期 
 
     */  
  
    private void updateDisplay() {  
  
        et_birthDate.setText(new StringBuilder().append(mYear).append(  
  
              (mMonth + 1) < 10 ? "0" + (mMonth + 1) : (mMonth + 1)).append(  
  
              (mDay < 10) ? "0" + mDay : mDay));  
  
    }  
	
	/** 
	 
     * 日期控件的事件 
 
     */  
  
    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {  
       @Override
       public void onDateSet(DatePicker view, int year, int monthOfYear,  
  
              int dayOfMonth) {  
  
           mYear = year;  
  
           mMonth = monthOfYear;  
  
           mDay = dayOfMonth;  
  
           updateDisplay();  
  
       }
    };
	
    @Override  
    
    protected Dialog onCreateDialog(int id) {  
  
       switch (id) {  
  
       case DATE_DIALOG_ID:  
  
           return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,  
  
                  mDay);  
  
       }  
  
       return null;  
  
    }  
    
    @Override  
    
    protected void onPrepareDialog(int id, Dialog dialog) {  
  
       switch (id) {  
  
       case DATE_DIALOG_ID:  
  
           ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);  
  
           break;  
  
       }  
  
    }  
	
    /** 
    
     * 处理日期控件的Handler 
 
     */  
  
    Handler saleHandler = new Handler() {  
  
       @Override  
  
       public void handleMessage(Message msg) {  
  
           switch (msg.what) {  
  
           case AddNewActivity.SHOW_DATAPICK:  
  
              showDialog(DATE_DIALOG_ID);  
  
              break;  
  
           }  
  
       }  
  
    };  
  
	/*
	public void loadImage() {
		if(imageChooseView == null) {
			LayoutInflater li = LayoutInflater.from(AddNewActivity.this);
			imageChooseView = li.inflate(R.layout.imageswitch, null);
			
			//ͨ����Ⱦxml�ļ����õ�һ����ͼ��View�������õ����View�����Gallery
			gallery = (Gallery)imageChooseView.findViewById(R.id.gallery);
			//ΪGalleryװ��ͼƬ
			gallery.setAdapter(new ImageAdapter(this));
			gallery.setSelection(images.length/2);
			is = (ImageSwitcher)imageChooseView.findViewById(R.id.imageswitch);
			is.setFactory(this);
			is.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
	        //ж��ͼƬ�Ķ���Ч��
	        is.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
			gallery.setOnItemSelectedListener(new OnItemSelectedListener(){

				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					//��ǰ��ͷ��λ��Ϊѡ�е�λ��
					currentImagePosition = arg2;
					//ΪImageSwitcher����ͼ��
					is.setImageResource(images[arg2 % images.length]);
					
				}
				public void onNothingSelected(AdapterView<?> arg0) {
					
				}});
		}
		
	}
	*/
	/**
	 * �Զ���Gallery��������
	 * @author Administrator
	 *
	 */
	class ImageAdapter extends BaseAdapter {

		private Context context;
		
		public ImageAdapter(Context context) {
			this.context = context;
		}
		
		public int getCount() {
			return Integer.MAX_VALUE;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		
		/**
		 * gallery���������õ�image
		 */
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView iv = new ImageView(context);
			//iv.setImageResource(images[position%images.length]);
			iv.setAdjustViewBounds(true);
			iv.setLayoutParams(new Gallery.LayoutParams(80,80));
			iv.setPadding(15, 10, 15, 10);
			return iv;
			
		}
		
	}

	
	public View makeView() {
		ImageView view = new ImageView(this);
		view.setBackgroundColor(0xff000000);
		view.setScaleType(ScaleType.FIT_CENTER);
		view.setLayoutParams(new ImageSwitcher.LayoutParams(90,90));
		return view;
	}
	
	public void initImageChooseDialog() {
		if(imageChooseDialog == null) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("��ѡ��ͼ��")
			.setView(imageChooseView).setPositiveButton("ȷ��", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					imageChanged = true;
					previousImagePosition = currentImagePosition;
					//imageButton.setImageResource(images[currentImagePosition%images.length]);
				}
			})
			.setNegativeButton("ȡ��", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					currentImagePosition = previousImagePosition;
					
				}
			});
			imageChooseDialog = builder.create();
		}
	}
	
	/**
	 * ���˳��ʱ�򣬻�����Դ
	 */
	protected void onDestroy() {
		if(is != null) {
			is = null;
		}
		if(gallery != null) {
			gallery = null;
		}
		if(imageChooseDialog != null) {
			imageChooseDialog = null;
		}
		if(imageChooseView != null) {
			imageChooseView = null;
		}
		if(imageButton != null) {
			imageButton = null;
		}
		if(manager != null){
		    manager.close();
		}
		super.onDestroy();
	}
}
